package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.Sanpham;
import util.ConvertHelper;
import dao.HoadonDAO;
import dao.SanphamDAO;

/**
 * Servlet implementation class TrangHoaDon
 */
@WebServlet("/Pages/TrangHoaDon.do")
public class TrangHoaDon extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TrangHoaDon() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		HttpSession session = request.getSession();

		String action = request.getParameter("action");
		String url = "";

		if (action.equals("DatHang")) {

			ArrayList<Sanpham> ds = (ArrayList<Sanpham>) session
					.getAttribute("gioHang");
			if (null != ds && ds.size() > 0) {
				request.setAttribute("page", "TrangHoaDon");

				// String tenNguoiNhan = ConvertHelper.GetRequestUnicodeString(
				// request, "tenNguoiNhan");
				String diaChiNguoiNhan = ConvertHelper.GetRequestUnicodeString(
						request, "diaChiNguoiNhan");
				// String soDienThoai = ConvertHelper.GetRequestUnicodeString(
				// request, "soDienThoai");

				// request.setAttribute("tenNguoiNhan", tenNguoiNhan);
				request.setAttribute("diaChiNguoiNhan", diaChiNguoiNhan);
				// request.setAttribute("soDienThoai", soDienThoai);
			} else {

				request.setAttribute("page", "TrangGioHang");
			}
			url = "../index.jsp";
		}
/*
		if (action.equals("TroLaiGioHang")) {

			request.setAttribute("page", "TrangGioHang");
			url = "../index.jsp";
		}
		
		 * if (action.equals("ThanhToan")) {
		 * 
		 * // --------------------- HoadonDAO dao = new HoadonDAO(); int
		 * maHoaDon = dao.ThemHoaDonMoi(request, session); //
		 * ---------------------
		 * 
		 * request.setAttribute("maHoaDon", maHoaDon);
		 * session.removeAttribute("gioHang");
		 * 
		 * request.setAttribute("page", "TrangHoanTatThanhToan"); url =
		 * "../index.jsp"; }
		 */
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

}
